package cn.newtest.bi.collection.dao.hibernate;

import java.util.List;

import net.risesoft.risebi.collection.dao.CatalogDao;
import net.risesoft.risebi.collection.model.Catalog;

import org.appfuse.dao.hibernate.BaseDaoHibernate;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
/**
*
* @author <a href="mailto: wangchui@risesoft.net">王锤</a>
* @version $Revision$
* 
*/
@Repository(value = "catalogDao")
public class CatalogDaoImpl extends BaseDaoHibernate implements CatalogDao {
	
	@Autowired
	public CatalogDaoImpl(SessionFactory sessionFactory) {
		super();
		setSessionFactory(sessionFactory);
	}
	
	public List<Catalog> getAllOrderByTabIndex() throws Exception {
		String hql = "from Catalog order by tabIndex";
		this.getHibernateTemplate().setCacheQueries(true);
		List<Catalog> list = this.getHibernateTemplate().find(hql);
		return list;
	}
	
	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.dao.CatalogDao#getCatalogWithSo()
	 */
	public List getCatalogWithSo() throws Exception {
		String hql = "from Catalog c where c.type=1 order by tabIndex";
		this.getHibernateTemplate().setCacheQueries(true);
		List<Catalog> list = this.getHibernateTemplate().find(hql);
		return list;
	}
	
	
	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.dao.CatalogDao#getCatalogWithSo()
	 */
	public List getCatalogWithSoByApp(String appGuid, boolean includeVirtual) throws Exception {
		String hql = "from Catalog c where c.type=1 and c.application.guid='" + appGuid + "' and (c.virtual is null or c.virtual=false) order by tabIndex";
		if(includeVirtual==true){
			hql = "from Catalog c where c.type=1 and c.application.guid='" + appGuid + "' order by tabIndex";
		}
		this.getHibernateTemplate().setCacheQueries(true);
		List<Catalog> list = this.getHibernateTemplate().find(hql);
		return list;
	}
	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.dao.CatalogDao#getCatalogWithSoByIds(java.lang.String[])
	 */
	public List getCatalogWithSoByIds(String[] catalogIdArray) throws Exception {
		String hql = "from Catalog c where ";
		for(int i=0;i<catalogIdArray.length;i++){
			hql += (i==0?"c.guid='"+catalogIdArray[i]+"'":" or c.guid='"+catalogIdArray[i]+"'");
		}
		
		this.getHibernateTemplate().setCacheQueries(true);
		
		List list = this.getHibernateTemplate().find(hql);
		return list;
	}

	public List<Catalog> getCatalogByType(int type) throws Exception {
		String hql = "from Catalog c where c.type=" + type + " order by tabIndex";
		this.getHibernateTemplate().setCacheQueries(true);
		List<Catalog> list = this.getHibernateTemplate().find(hql);
		return list;
	}
	
	public List<Catalog> getCatalogByApp(String appGuid,String[] filter) throws Exception {
		String hql = "from Catalog c where c.application.guid='" + appGuid + "' ";
		if(filter!=null){
			for(String str:filter){
				hql += " and c.tableName not like '" + str + "%' ";
			}
		}
		List<Catalog> list = this.getHibernateTemplate().find(hql);
		return list;
	}

}
